package com.chengxusheji.action;

 
import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.chengxusheji.dao.AdminDAO;
import com.chengxusheji.dao.BusinessCompanyDAO;
import com.chengxusheji.domain.Admin;

@Controller @Scope("prototype")
public class LoginAction extends ActionSupport {
 

	@Resource AdminDAO adminDAO; 
	@Resource BusinessCompanyDAO bcDAO;

	private Admin admin;

	//用户身份
	private String identify;
	
	public String getIdentify() {
		return identify;
	}

	public void setIdentify(String identify) {
		this.identify = identify;
	}

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	/*直接跳转到登陆界面*/
	public String view() {
		
		return "login_view";
	}
	 
	
	/* 验证用户登录 */
	public String CheckLogin() { 
		ActionContext ctx = ActionContext.getContext();
		
		if(identify.equals("admin")) {
			if (!adminDAO.CheckLogin(admin)) {
				ctx.put("error",  java.net.URLEncoder.encode(adminDAO.getErrMessage()));
				return "error";
			}
			ctx.getSession().put("username", admin.getUsername());
			return "main_view";
		} else {
			if (!bcDAO.CheckLogin(admin.getUsername(),admin.getPassword())) {
				ctx.put("error",  java.net.URLEncoder.encode(bcDAO.getErrMessage()));
				return "error";
			}
			ctx.getSession().put("username", admin.getUsername());
			return "business_main_view";
		}
		
 
	}

	

}
